/*
 * @Author: szx
 * @Date: 2022-05-02 12:56:42
 * @LastEditTime: 2022-05-02 13:04:13
 * @Description:
 * @FilePath: \leetcode\600-699\695\695.js
 */
/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function (grid) {
    const dirs = [
        [-1, 0],
        [1, 0],
        [0, -1],
        [0, 1]
    ];
    const m = grid.length;
    const n = grid[0].length;
    const used = new Array(m).fill(false).map(() => new Array(n).fill(false));
    const dfs = (row, col) => {
        if (used[row][col] || grid[row][col] == 0) return 0;
        let res = 1;
        for (let dir of dirs) {
            const x = row + dir[0];
            const y = col + dir[1];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[row][col] == 1) {
                res += dfs(x, y);
            }
        }
        return res;
    };
    let max = 0;
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            Math.max(max, dfs(i, j));
        }
    }
    return max;
};
